package com.yurendao.shop.aspect.user;

import com.yurendao.shop.user.domain.User;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;

/**
 * Created by kong on 15/9/30.
 * 用户密码加密
 */
@Aspect
public class Encoding {

    private Md5PasswordEncoder encoder = new Md5PasswordEncoder();

    @Before("execution(* com.yurendao.shop.user.service.*.insert*(..))")
    public void encode(JoinPoint joinPoint){
        if(joinPoint.getArgs()[0] instanceof User){
            User user = (User) joinPoint.getArgs()[0];
            String upass = user.getPassword();
            String uname = user.getNickname();
            String encodepass = encoder.encodePassword(uname,uname);
            user.setPassword(upass);
        }
    }

}
